/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.*;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Lee
 */
public class AppraisalDAO extends DAO {

    public AppraisalDAO() {
        super();
    }

    //BASIC DAO METHODS...
    @SuppressWarnings("unchecked")
    public List<Appraisal> getAll() throws Exception {
        List<Appraisal> a = (List<Appraisal>) getSession().createQuery("from Appraisal a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Appraisal get(int id) throws Exception {
        Appraisal a = (Appraisal) getSession().createQuery("from Appraisal a where a.appraisalid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Appraisal a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Appraisal a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Appraisal> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Appraisal> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }

    //Other Methods
    @SuppressWarnings("unchecked")
    public void addAppraisal(Documents d, Appraisal a, List<Collaterals> listcol) throws Exception {
        try {
            Session ses = getSession();
            begin();
            ses.save(d);
            ses.save(a);
            for (int i = 0; i < listcol.size(); i++) {
                ses.save(listcol.get(i));
            }
            commit();
            flush();
        } catch (Exception ex) {
            rollback();
            throw ex;
        }
    }

    @SuppressWarnings("unchecked")
    public Appraisal getLatestByLoanId(int loanappid) throws Exception {
        Appraisal c = (Appraisal) getSession().createQuery("from Appraisal c where c.loanapplications.loanappid=:lid order by c.appraisalid desc").setParameter("lid", loanappid).setMaxResults(1).uniqueResult();
        flush();
        return c;
    }
    
    @SuppressWarnings("unchecked")
    public Appraisal getLatestByLoanverId(int loanverid) throws Exception {
        Appraisal c = (Appraisal) getSession().createQuery("from Appraisal c where c.loanverifications.loanverificationid=:lid order by c.appraisalid desc")
                    .setParameter("lid", loanverid).setMaxResults(1).uniqueResult();
        flush();
        return c;
    }

    @SuppressWarnings("unchecked")
    public List<Collaterals> getCollsPerAppraisalId(int apprid) throws Exception {
        List<Collaterals> c = (List<Collaterals>) getSession().createQuery("from Collaterals c where c.appraisal.appraisalid=:aid").setParameter("aid", apprid).setCacheable(true).list();
        //System.out.println("collsperappr " + c.get(1).getCollateralid().toString());
        flush();
        return c;
    }
}
